#include "stm32f10x.h"
#include "OLED.h"
#include "HCSR04.h"
#include "DHT11.h"
#include "usart.h"
#include "stdio.h"
#include "string.h"
//超声波读取距离
int main()
{
	float Length,temp,humi,coef;
	// uint8_t longd;
	uint16_t send_length,send_temp,send_humi;
	uint8_t send_arr[9];
	// char buff[15];
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_RESET);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//NVIC优先级分组设置

	IIC1_Init();
	OLED_Init();
	OLED_Clear();
	HCSR04_Init();
	USART1_Init();

	OLED_Show_String(1,1,"length:");
	OLED_Show_String(1,15,"cm");
	OLED_Show_String(2,1,"Temp:");
	OLED_Show_String(3,1,"Humi:");
	OLED_Show_String(4,1,"Coef:");

	while (1)
	{
		Length = HCSR04_GetDisdance();
		// if (Length > 1000)
		// {
		// 	Length = Length_1;
		// }
		DHT11_RecData(&temp,&humi);
		coef = 0.00061*temp+0.33145;
		Length = coef*Length/2;
		if (Length <1000)
		{
			send_length = (uint16_t)(Length*100);
		}
		else
		{
			send_length = 0x0000;
		}
		
		send_humi = (uint16_t)(humi*100);
		send_temp = (uint16_t)(temp*100);
		send_arr[0] = 0xfe;
		send_arr[1] = 7;
		send_arr[2] = send_length / 256;
		send_arr[3] = send_length % 256;
		send_arr[4] = send_temp / 256;
		send_arr[5] = send_temp % 256;
		send_arr[6] = send_humi / 256;
		send_arr[7] = send_humi % 256;
		send_arr[8] = 0xff;

		USART1_SendString(send_arr,9);

		// memset(buff, 0, sizeof(buff));   
		// sprintf(buff,",%.2f\r\n",Length);
		// longd = strlen(buff);
		// printf("%d%s",longd,buff);


		// USART1_SendString(&Length,1);
		OLED_Show_PointNum(1,8,Length,4,2);
		OLED_Show_PointNum(2,6,temp,2,2);
		OLED_Show_PointNum(3,6,humi,2,2);
		OLED_Show_PointNum(4,6,coef,1,5);
	}
}
